package shajie.shajun.mmhf.ViewModel;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import Net.Error;
import Net.ResponseHandler;
import Util.Util;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import shajie.shajun.mmhf.Bean2.OutGoods;
import shajie.shajun.mmhf.Bean2.PostPriceResult;
import shajie.shajun.mmhf.Bean2.Product;
import shajie.shajun.mmhf.Bean2.Result;
import shajie.shajun.mmhf.Bean2.SearchOrder;
import shajie.shajun.mmhf.Interface.Getdata;
import shajie.shajun.mmhf.Public_Data;
import shajie.shajun.mmhf.ViewModel.Net.HttpClient;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2017/11/6.
 */

public class Bmobdata {
  public interface Bmob_Modify{
        void faile(String s);
    }
    public interface Bmob_GetonLineTime{
        void success(String[] user,ArrayList<Integer> num);
    }
    public interface Singer<T>{
        void success(T s);
        void faile(String s);
    }

    //通知任务工资等相关信息
    public static void searchdata(final Context context,final Getdata getdata){

    }

    //搜索淘宝号相关订单信息
    public static void getsearchorderdata(Context context,String title,final Singer<List<SearchOrder>> singer){
//        BmobQuery<Order> query = new BmobQuery<Order>();
////查询playerName叫“比目”的数据
//        query.addWhereEqualTo("taobaocount", title);
//        query.order("-Datetime");
////返回50条数据，如果不加上这条语句，默认返回10条数据
//        query.setLimit(500);
////执行查询方法
//        query.findObjects(new FindListener<Order>() {
//            @Override
//            public void done(List<Order> object, BmobException e) {
//                if(e==null){
//                   singer.success(object);
//                    Log.e(TAG, "done: "+object.size() );
//                }else{
//                    Log.e("bmob","失败："+e.getMessage()+","+e.getErrorCode());
//                }
//            }
//        });
        HttpClient client=new HttpClient(context);
        Call<List<SearchOrder>> call=client.service(ISjun.urltaobao).getSearchOrder("SearchOrder",title);
        client.request(call,new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((List<SearchOrder>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });
    }

    //搜索联系人
    public static void getSearchPerson(Context context,String title,final Singer<List<SearchOrder>> singer) {
        HttpClient client=new HttpClient<>(context);
        Call<List<SearchOrder>> calls=client.service(ISjun.urltaobao).getSearchPerson("SearchPerson",title);
        client.request(calls, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((List<SearchOrder>) o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });
    }


//    //产品详情
//    public static void getproductdetail(final Singer<List<Products>> singer){
//        BmobQuery<Products> query = new BmobQuery<Products>();
//        query.order("Sort");
//        query.setLimit(500);
////执行查询方法
//        query.findObjects(new FindListener<Products>() {
//            @Override
//            public void done(List<Products> object, BmobException e) {
//                if(e==null){
//                        singer.success(object);
//                        Log.e(TAG, "done: " + object.size());
//                }else{
//                    singer.faile("失败："+e.getMessage()+","+e.getErrorCode());
//                    Log.e("bmob","失败："+e.getMessage()+","+e.getErrorCode());
//                }
//            }
//        });
//    }
    //产品详情
    public static void getproductdetail(Context context,final Singer<List<List<Product>>> singer) {
        HttpClient client=new HttpClient<>(context);
        Call<List<List<Product>>> calls=client.service(ISjun.urltaobao).getProducts("Product");
        client.request(calls, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((List<List<Product>>) o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });
    }
    //最近30天订单
//    public static void get30order(final Singer<List<Order>> singer){
//        Calendar cdate = Calendar.getInstance();
//        cdate.add(Calendar.DATE,-30);
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        String startriqi=sdf.format(cdate.getTime());
//        BmobQuery<Order> query = new BmobQuery<Order>();
//        query.addWhereGreaterThan("Datetime", startriqi);
////查询playerName叫“比目”的数据
//        query.order("-Datetime");
////返回50条数据，如果不加上这条语句，默认返回10条数据
//        query.setLimit(500);
////执行查询方法
//        query.findObjects(new FindListener<Order>() {
//            @Override
//            public void done(List<Order> object, BmobException e) {
//                if(e==null){
//                    singer.success(object);
//                    Log.e(TAG, "done: "+object.size() );
//                }else{
//                    Log.e("bmob","失败："+e.getMessage()+","+e.getErrorCode());
//                }
//            }
//        });
//    }
    public static void get30order(Context context,final Singer<List<SearchOrder>> singer){
        HttpClient client=new HttpClient(context);
        Call<List<SearchOrder>> call=client.service(ISjun.urltaobao).getRecentOrder("RecentOrder");
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((List<SearchOrder>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });
    }
    //修改库存
    public static void modifyDatabase(Context context,String n,String name,final Singer<Result> singer){
        HttpClient client=new HttpClient(context);
        Call<Result> call=client.service(ISjun.urltaobao).modifyProductDatabse("ModifyInventory",n,name);
        Log.e(TAG, "modifyDatabase: "+call.request().url().toString() );
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((Result)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });
    }
    //获取图片
    public static void getpicture(Context context,String id,final Singer<Result> singer){
        HttpClient client=new HttpClient(context);
        Call<Result> call=client.service(ISjun.urltaobao).getpicture(id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((Result)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });
    }
    //上传图片
    public static void uppictures(final Context context, final List<String> parts, final Singer<Result> singer){
        final HttpClient client=new HttpClient<>(context);

            Util.compressMore(context, parts, new Interface.MCallBack() {
                @Override
                public void succes(String s) {
                    if (Public_Data.files==null){
                        Public_Data.files=new ArrayList<>();
                    }
                    Public_Data.files.add(s);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),  new File(s));
                    MultipartBody.Part  part=MultipartBody.Part.createFormData("2018-1-25", new File(s).getName(),requestBody);
                    Call<Result> calls=client.service(ISjun.urltaobao).uploadPictures(part);
                    client.request(calls, new ResponseHandler() {
                        @Override
                        public void onSuccess(Object o) {
                            Log.e(TAG, "uppictures:______ " );
                            singer.success((Result)o);
                        }
                        @Override
                        public void onFailure(int code, Error e) {
                            singer.faile(e.getMessage());
                        }
                    });
                }
            });

    }
    //发货数据
    public static void OutGoods(final Context context, final Singer<List<OutGoods>> singer){
        final HttpClient client=new HttpClient<>(context);
        Call<List<OutGoods>> call=client.service(ISjun.urltaobao).OutGoods("Consignment");
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((List<OutGoods>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });


    }
    //添加发货数据
    public static void addOutGoods(final Context context, Map<String,String> maps,final Singer<Result> singer){
        final HttpClient client=new HttpClient<>(context);
        Call<Result> call=client.service(ISjun.urltaobao).addOutGoods(maps);
        Log.e(TAG, "addOutGoods: "+  call.request().url().toString() );
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((Result)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });


    }
    //计算邮费
    public static void computepostprice(final Context context, Map<String,String> maps,final Singer<PostPriceResult> singer){
        final HttpClient client=new HttpClient<>(context);
        Call<PostPriceResult> call=client.service(ISjun.urltaobao).postprice(maps);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) {
                singer.success((PostPriceResult)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.faile(e.getMessage());
            }
        });


    }


}
